﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Project.Models;

namespace Project.Controllers
{
    public class WarehouseItemsController : Controller
    {
        private projektlaborEntities db = new projektlaborEntities();

        // GET: WarehouseItems, ahol PickingStock, ReserveStock lehet, de DeleteDate == null
        public async Task<ActionResult> Index()
        {
            var warehouseItems = db.WarehouseItems.Where(w => w.DateDelete == null).OrderBy(w => w.Item_Id).Include(w => w.ItemStock).Include(w => w.PickingStock).Include(w => w.ReserveStock).Include(w => w.Spaces);
            return View(await warehouseItems.ToListAsync());
        }
        // GET: WarehouseItemsDeleted, kitörölt elemek
        public async Task<ActionResult> DeletedIndex()
        {
            var warehouseItems = db.WarehouseItems.Where(w => w.DateDelete != null && w.ReserveStock_Id == null && w.PickingStock_Id == null).OrderBy(w => w.Item_Id).Include(w => w.ItemStock).Include(w => w.PickingStock).Include(w => w.ReserveStock).Include(w => w.Spaces);
            return View(await warehouseItems.ToListAsync());
        }

        // GET: ReserveStock Index
        public async Task<ActionResult> ReserveStock()
        {
            var warehouseItems = db.WarehouseItems.Where(w => w.ReserveStock_Id != null && w.DateDelete != null).OrderBy(w => w.Item_Id).Include(w => w.ItemStock).Include(w => w.PickingStock).Include(w => w.ReserveStock).Include(w => w.Spaces);
            return View(await warehouseItems.ToListAsync());
        }
        // GET: PickupStock Index
        public async Task<ActionResult> PickingStock()
        {
            var warehouseItems = db.WarehouseItems.Where(w => w.PickingStock_Id != null && w.DateDelete != null).OrderBy(w => w.Item_Id).Include(w => w.ItemStock).Include(w => w.PickingStock).Include(w => w.ReserveStock).Include(w => w.Spaces);
            return View(await warehouseItems.ToListAsync());
        }
        // GET: TakoutIndex, Kivétel index
        public async Task<ActionResult> TakeoutIndex()
        {
            var warehouseItems = db.WarehouseItems.Where(w => w.PickingStock_Id == null && w.ReserveStock_Id == null && w.DateDelete == null).OrderBy(w => w.Item_Id).Include(w => w.ItemStock).Include(w => w.PickingStock).Include(w => w.ReserveStock).Include(w => w.Spaces);
            return View(await warehouseItems.ToListAsync());
        }
        // GET: ItemStock/Details/5 a raktárcikk listázásához a RenderAction ezt hívja meg
        public ActionResult ItemStockDetails(int? id)
        {
            return PartialView("ItemStockDetails", id);
        }





        // GET: WarehouseItems/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WarehouseItems warehouseItems = db.WarehouseItems.Find(id);
            ItemStock itemStock = db.ItemStock.Find(warehouseItems.Item_Id);
            if (warehouseItems == null)
            {
                return HttpNotFound();
            }
            return View(warehouseItems);
        }







        // GET: WarehouseItems/Create
        public ActionResult Create()
        {
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName");
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName");
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName");
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id");
            return View();
        }

        // POST: WarehouseItems/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "WarehouseItem_Id,Item_Id,PickingStock_Id,ReserveStock_Id,Space_Id,Quantity,Unit,ItemNumber,Note,UserIn,DateIn,DateDelete,UserDelete")] WarehouseItems warehouseItems)
        {
            if (ModelState.IsValid)
            {
                db.WarehouseItems.Add(warehouseItems);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName", warehouseItems.Item_Id);
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName", warehouseItems.PickingStock_Id);
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName", warehouseItems.ReserveStock_Id);
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id", warehouseItems.Space_Id);
            return View(warehouseItems);
        }









        // GET: WarehouseItems/Takein bevételezés
        public ActionResult Takein()
        {
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName");
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName");
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName");
            ViewBag.Space_Id = new SelectList(db.Spaces.Where(w => w.IsFull != true), "Space_Id", "Space_Id");
            return View();
        }
        // POST: WarehouseItems/Takein bevételezés
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Takein([Bind(Include = "WarehouseItem_Id,Item_Id,PickingStock_Id,ReserveStock_Id,Space_Id,Quantity,Unit,ItemNumber,Note,UserIn,DateIn,DateDelete,UserDelete")] WarehouseItems warehouseItems)
        {
            if (ModelState.IsValid)
            {
                db.WarehouseItems.Add(warehouseItems);
                await db.SaveChangesAsync();
                return RedirectToAction("FreeIndex", "Spaces", new {warehouseitemid = warehouseItems.WarehouseItem_Id});
            }

            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName", warehouseItems.Item_Id);
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName", warehouseItems.PickingStock_Id);
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName", warehouseItems.ReserveStock_Id);
            ViewBag.Space_Id = new SelectList(db.Spaces.Where(w => w.IsFull != true), "Space_Id", "Space_Id", warehouseItems.Space_Id);
            return View(warehouseItems);
        }
        // GET: WarehouseItems/Takeout a komissióra
        public async Task<ActionResult> TakeoutToReserve(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WarehouseItems warehouseItems = await db.WarehouseItems.FindAsync(id);
            if (warehouseItems == null)
            {
                return HttpNotFound();
            }
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName");
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName");
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName");
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id");
            return View(warehouseItems);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        // POST: WarehouseItems/Takeout a komissióra
        public async Task<ActionResult> TakeoutToReserve([Bind(Include = "WarehouseItem_Id,Item_Id,ReserveStock_Id,DateDelete,UserDelete")] WarehouseItems warehouseItems)
        {
            if (ModelState.IsValid)
            {
                WarehouseItems warehouseItemsOrig = await db.WarehouseItems.FindAsync(warehouseItems.WarehouseItem_Id);
                warehouseItemsOrig.Space_Id = null;
                warehouseItemsOrig.DateDelete = warehouseItems.DateDelete;
                warehouseItemsOrig.UserDelete = warehouseItems.UserDelete;
                warehouseItemsOrig.ReserveStock_Id = warehouseItems.ReserveStock_Id;
                db.Entry(warehouseItemsOrig).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("ReserveStock");
            }
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName", warehouseItems.Item_Id);
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName", warehouseItems.PickingStock_Id);
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName", warehouseItems.ReserveStock_Id);
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id", warehouseItems.Space_Id);
            return View(warehouseItems);
        }
        // GET: WarehouseItems/Takeout a kirakodásra
        public async Task<ActionResult> TakeoutToPicking(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WarehouseItems warehouseItems = await db.WarehouseItems.FindAsync(id);
            if (warehouseItems == null)
            {
                return HttpNotFound();
            }
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName");
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName");
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName");
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id");
            return View(warehouseItems);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        // POST: WarehouseItems/Takeout a kirakodásra
        public async Task<ActionResult> TakeoutToPicking([Bind(Include = "WarehouseItem_Id,Item_Id,PickingStock_Id,ReserveStock_Id,Space_Id,Quantity,Unit,ItemNumber,Note,UserIn,DateIn,DateDelete,UserDelete")] WarehouseItems warehouseItems)
        {
            if (ModelState.IsValid)
            {
                WarehouseItems warehouseItemsOrig = await db.WarehouseItems.FindAsync(warehouseItems.WarehouseItem_Id);
                warehouseItemsOrig.Space_Id = null;
                warehouseItemsOrig.DateDelete = warehouseItems.DateDelete;
                warehouseItemsOrig.UserDelete = warehouseItems.UserDelete;
                warehouseItemsOrig.PickingStock_Id = warehouseItems.PickingStock_Id;
                db.Entry(warehouseItemsOrig).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("PickingStock");
            }
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName", warehouseItems.Item_Id);
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName", warehouseItems.PickingStock_Id);
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName", warehouseItems.ReserveStock_Id);
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id", warehouseItems.Space_Id);
            return View(warehouseItems);
        }

        //Visszahelyezés a raktárva
        public async Task<ActionResult> Takeback(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WarehouseItems warehouseItems = await db.WarehouseItems.FindAsync(id);
            if (warehouseItems == null)
            {
                return HttpNotFound();
            }
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName", warehouseItems.Item_Id);
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName", warehouseItems.PickingStock_Id);
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName", warehouseItems.ReserveStock_Id);
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id", "Space_Id");
            return View(warehouseItems);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Takeback([Bind(Include = "WarehouseItem_Id,Item_Id,PickingStock_Id,ReserveStock_Id,Space_Id,Quantity,Unit,ItemNumber,Note,UserIn,DateIn,DateDelete,UserDelete")] WarehouseItems warehouseItems)
        {
            if (ModelState.IsValid)
            {
                warehouseItems.DateDelete = null;
                warehouseItems.UserDelete = null;
                warehouseItems.PickingStock_Id = null;
                warehouseItems.ReserveStock_Id = null;
                db.Entry(warehouseItems).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("FreeIndex", "Spaces", new { warehouseitemid = warehouseItems.WarehouseItem_Id });
            }
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName", warehouseItems.Item_Id);
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName", warehouseItems.PickingStock_Id);
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName", warehouseItems.ReserveStock_Id);
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id", warehouseItems.Space_Id);
            return View();
        }








        // GET: WarehouseItems/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WarehouseItems warehouseItems = await db.WarehouseItems.FindAsync(id);
            if (warehouseItems == null)
            {
                return HttpNotFound();
            }
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName", warehouseItems.Item_Id);
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName", warehouseItems.PickingStock_Id);
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName", warehouseItems.ReserveStock_Id);
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id", warehouseItems.Space_Id);
            return View(warehouseItems);
        }

        // POST: WarehouseItems/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "WarehouseItem_Id,Item_Id,PickingStock_Id,ReserveStock_Id,Space_Id,Quantity,Unit,ItemNumber,Note,UserIn,DateIn,DateDelete,UserDelete")] WarehouseItems warehouseItems)
        {
            if (ModelState.IsValid)
            {
                db.Entry(warehouseItems).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.Item_Id = new SelectList(db.ItemStock, "Item_Id", "ItemName", warehouseItems.Item_Id);
            ViewBag.PickingStock_Id = new SelectList(db.PickingStock, "PickingStock_Id", "PickingStockName", warehouseItems.PickingStock_Id);
            ViewBag.ReserveStock_Id = new SelectList(db.ReserveStock, "ReserveStock_Id", "ReserveStockName", warehouseItems.ReserveStock_Id);
            ViewBag.Space_Id = new SelectList(db.Spaces, "Space_Id", "Space_Id", warehouseItems.Space_Id);
            return View(warehouseItems);
        }

        // GET: WarehouseItems/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            WarehouseItems warehouseItems = await db.WarehouseItems.FindAsync(id);
            if (warehouseItems == null)
            {
                return HttpNotFound();
            }
            return View(warehouseItems);
        }

        // POST: WarehouseItems/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            WarehouseItems warehouseItems = await db.WarehouseItems.FindAsync(id);
            warehouseItems.PickingStock_Id = null;
            warehouseItems.ReserveStock_Id = null;
            warehouseItems.Space_Id = null;
            warehouseItems.DateDelete = Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd. HH:mm:ss"));
            db.Entry(warehouseItems).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
